import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store'
//import Cart from '@/pages/cart/Cart'
const Home = resolve => require(['@/module/home/App'], resolve)
const HomeContent = resolve => require(['@/module/home/Content'], resolve)
// const HelloForm = resolve => require(['@/components/HelloFromVux'], resolve)
const Shop = resolve => require(['@/module/detail/components/shop'], resolve) //shop
const vDetails = resolve => require(['@/module/detail/components/vDetails'], resolve) //vDetails
const vComment = resolve => require(['@/module/detail/components/vComment'], resolve)
const commentMain = resolve => require(['@/module/detail/components/commentMain'], resolve)//commentMain
const Imgdetail = resolve => require(['@/module/detail/components/imgdetail'], resolve)
const Parameter = resolve => require(['@/module/detail/components/parameter'], resolve)
const Aftermarket = resolve => require(['@/module/detail/components/aftermarket'], resolve)
const ImgContent = resolve => require(['@/module/detail/components/imgcontent'], resolve)
const My = resolve => require(['@/module/my/App'], resolve)
const Coupon = resolve => require(['@/module/my/components/coupon'], resolve)
const couponType = resolve => require(['@/module/my/components/coupontype'], resolve)
const volumeInfo = resolve => require(['@/module/my/components/volumeinfo'], resolve)
const List = resolve => require(['@/module/list/App'], resolve)

const ShopList = resolve => require(['@/module/list/components/shoplist/shoplist'], resolve)
const Searchshoplist = resolve => require(['@/module/list/components/shoplist/searchshoplist'], resolve)
const NeedShop = resolve => require(['@/module/list/components/needshop/needshop'], resolve)
// const Cart = resolve => require(['@/module/cart/App'], resolve)

// 购物车
const Cart1 = resolve => require(['@/module/cart1/App'], resolve)

const SuFreshCarts = resolve => require(['@/module/cart1/components/suFreshCarts'], resolve)
const CenterCarts  = resolve => require(['@/module/cart1/components/centerCarts'], resolve)

const Cart2 = resolve => require(['@/module/cart2/App'], resolve)
const Cart2conpon = resolve => require(['@/module/cart2/cart2conpon'], resolve)
const Cart2Centerconpon = resolve => require(['@/module/cart2/cart2centerconpon'], resolve)
const CenterCart2 = resolve => require(['@/module/cart2/centerCart2'], resolve)
const CartShopDetail = resolve => require(['@/module/cart2/shopDetail'], resolve)
const Invoice = resolve => require(['@/module/cart2/Invoice'], resolve)
const Cart3 = resolve => require(['@/module/cart3/App'], resolve)
const Cart4 = resolve => require(['@/module/cart4/App'], resolve)
const CenterCart4 = resolve => require(['@/module/cart4/centerCart4'], resolve)
const Detail = resolve => require(['@/module/detail/App'], resolve)
const Storedetail = resolve => require(['@/module/detail/components/storedetail'], resolve)
// 地址选择
const Address = resolve => require(['@/module/address/App'], resolve)
const myAddress = resolve => require(['@/module/address/children/myAddress'], resolve)
const nearby = resolve => require(['@/module/address/children/nearby'], resolve)
const edit = resolve => require(['@/module/address/children/edit'], resolve)
const all = resolve => require(['@/module/address/children/all'], resolve)

// 订单中心
const Orders = resolve => require(['@/module/orders/App'], resolve)
const Orders_list = resolve => require(['@/module/orders/orderlist'], resolve)
const Orders_others = resolve => require(['@/module/orders/others'], resolve)
const OrderDetail = resolve => require(['@/module/orders/detail'], resolve)
const OrderTrail = resolve => require(['@/module/orders/trail'], resolve)
const snOrderList = resolve => require(['@/module/orders/snOrderList'], resolve)
const snOrderDetail = resolve => require(['@/module/orders/snOrderDetail'], resolve)
const snOrderTrail = resolve => require(['@/module/orders/snOrderTrail'], resolve)


import { loadScript, loadStyles, SetTitle } from '@/config/utils'

Vue.use(Router)

let _this = Vue;

const router =  new Router({
	routes: [
		{ 
			path: '/', 
			component: Home,
			children: [
		        { path: '/index/:type', component: HomeContent },
		    ],
		    beforeEnter: (to, from, next) => {
				if (typeof Swiper == 'undefined') {
					loadStyles("static/plugins/swiper/swiper.min.css");
		            loadScript("static/plugins/swiper/swiper.min.js");
		        }
		        store.commit('updateShowFooter', {showFooter: true})
				SetTitle('苏鲜生')
				next();
			}
		},
		{
			path: '/address',
			component: Address,
			children: [
				{path: '',name: 'myaddress',component: myAddress},
				{path: 'nearby',name:"nearby",component: nearby},
			],
			beforeEnter: (to, from, next) => {
				store.commit('updateShowFooter', {showFooter: false})
				SetTitle('地址选择')
				next();
			}
		},
		{
			path: '/cartAddress',
			component: myAddress,
			beforeEnter: (to, from, next) => {
				store.commit('updateShowFooter', {showFooter: false})
				SetTitle('我的收货地址')
				next();
			}
		},
		{
			path: '/cartAddress/:type',
			component: myAddress,
			beforeEnter: (to, from, next) => {
				store.commit('updateShowFooter', {showFooter: false})
				SetTitle('我的收货地址')
				next();
			}
		},
		{
			path: '/coupon',
			component: Coupon,
			beforeEnter: (to, from, next) => {
				store.commit('updateShowFooter', {showFooter: false})
				SetTitle('优惠券')
				next();
			},
			children: [{ 
				path: ':type', 
				component: couponType
			}]
		},
		{
			path: '/volumeinfo',
			component: volumeInfo,
			beforeEnter: (to, from, next) => {
				store.commit('updateShowFooter', {showFooter: false})
				SetTitle('券使用说明')
				next();
			}
		},
		{
			path: '/address/edit',
			component: edit,
			beforeEnter: (to, from, next) => {
				store.commit('updateShowFooter', {showFooter: false})
				next();
			}
		},
		{
			path: '/address/edit/:type',
			component: edit,
			beforeEnter: (to, from, next) => {
				store.commit('updateShowFooter', {showFooter: false})
				next();
			}
		},
		{
			path: '/address/all/:type',
			component: all,
			beforeEnter: (to, from, next) => {
				store.commit('updateShowFooter', {showFooter: false})
				next();
			}
		},
		{ 
			path: '/index', 
			name: 'index',
			component: Home,
			beforeEnter: (to, from, next) => {
				if (typeof Swiper == 'undefined') {
					loadStyles("static/plugins/swiper/swiper.min.css");
		            loadScript("static/plugins/swiper/swiper.min.js");
		        }
		        store.commit('updateShowFooter', {showFooter: true})
				SetTitle('苏鲜生')
				localStorage.top = 0
				next();
			},
			children: [{ 
				path: ':type', 
				component: HomeContent
			}]
		},
		{ 
			path: '/list', 
			component: List ,
			beforeEnter: (to, from, next) => {
				store.commit('updateShowFooter', {showFooter: true})
				SetTitle('分类')
				next();
			}
			
		},
		{ 
			path: '/cart1', 
			component: Cart1 ,
			beforeEnter: (to, from, next) => {
				store.commit('updateShowFooter', {showFooter: true})
				SetTitle('购物车')
				localStorage.top = 0
				next();
			},
			children: [{
					path: '', 
					component: SuFreshCarts,
				},
				{
					path: '/cart1/center', 
					component: CenterCarts,
			}]
			
		},
		{ 
			path: '/cart2', 
			component: Cart2 ,
			beforeEnter: (to, from, next) => {
				store.commit('updateShowFooter', {showFooter: false})
				SetTitle('购物车')
				next();
			}

		},
		{ 
			path: '/cart2conpon', 
			component: Cart2conpon ,
			beforeEnter: (to, from, next) => {
				store.commit('updateShowFooter', {showFooter: false})
				SetTitle('优惠券')
				next();
			}
		},
		{ 
			path: '/cart2centerconpon', 
			component: Cart2Centerconpon ,
			beforeEnter: (to, from, next) => {
				store.commit('updateShowFooter', {showFooter: false})
				SetTitle('优惠券')
				next();
			}
		},
		{ 
			path: '/cart2/invoice/:id', 
			component: Invoice ,
			beforeEnter: (to, from, next) => {
				store.commit('updateShowFooter', {showFooter: false})
				SetTitle('发票')
				next();
			}
			
		},
		{ 
			path: '/cart2/shopDetail', 
			component: CartShopDetail ,
			beforeEnter: (to, from, next) => {
				store.commit('updateShowFooter', {showFooter: false})
				SetTitle('商品清单')
				next();
			}
			
		},
		
		{ 
			path: '/cart2/center/:cart2No', 
			component: CenterCart2 ,
			beforeEnter: (to, from, next) => {
				store.commit('updateShowFooter', {showFooter: false})
				SetTitle('购物车')
				next();
			}
		},
		{ 
			path: '/cart3/:orderid', 
			component: Cart3 ,
			beforeEnter: (to, from, next) => {
				store.commit('updateShowFooter', {showFooter: false})
				SetTitle('购物车')
				next();
			}
			
		},
		{ 
			path: '/cart4/:orderid', 
			component: Cart4 ,
			beforeEnter: (to, from, next) => {
				store.commit('updateShowFooter', {showFooter: false})
				SetTitle('购物车')
				next();
			}
			
		},
		{ 
			path: '/cart4/center/:orderid', 
			component: CenterCart4 ,
			beforeEnter: (to, from, next) => {
				store.commit('updateShowFooter', {showFooter: false})
				SetTitle('购物车')
				next();
			}
			
		},
		{ 
			path: '/my', 
			component: My ,
			beforeEnter: (to, from, next) => {
				store.commit('updateShowFooter', {showFooter: true})
				localStorage.top = 0
				SetTitle('个人中心')

				next();

			}
		},
		{ 
			path: '/detail/:cmmdtyCode/:supplierCode', 
			component: Detail,
			beforeEnter: (to, from, next) => {
				// 商户编码截取前10位，防止url后面带上其他信息
				to.params.supplierCode = to.params.supplierCode.substr(0, 10);
				store.commit('updateShowFooter', {showFooter: false})
				store.commit('updateDirection', {direction: 'forward'})
				SetTitle('商品详情')
				next();
			},
			children: [{
				path: '', 
				component: Shop
			},
			{
				path: 'storedetail', 
				component: Storedetail
			},
			{
				path: 'vdetails', 
				component: vDetails,
				children:[{ 
					path: 'imgdetail', 
					component: Imgdetail
				},
				{ 
					path: 'parameter', 
					component: Parameter
				},
				{ 
					path: 'aftermarket', 
					component: Aftermarket
				}]
			},
			{
				path: 'vcomment', 
				component: vComment,
				children:[{
					path: ':commentname',//评价的名字 
					component: commentMain
				}]
			}
			]
		},
		{ 
			name:'imgcontent',
			path: '/imgcontent/:imgid', 
			component: ImgContent,
			beforeEnter: (to, from, next) => {
				store.commit('updateShowFooter', {showFooter: false})
				store.commit('updateDirection', {direction: 'forward'})
				next();
			}
		},
		{
			name:'shoplist',
			path: '/shoplist/:catalogCode',
			component:ShopList,
			beforeEnter: (to, from, next) => {
				store.commit('updateShowFooter', {showFooter: false})
				store.commit('updateDirection', {direction: 'forward'})
				next();
			},
			children: [{
				path: 'all', 
				component: ShopList
			}]
		},
		{
			name:'searchshoplist',
			path: '/searchshoplist',
			component:Searchshoplist,
			beforeEnter: (to, from, next) => {
				store.commit('updateShowFooter', {showFooter: false})
				store.commit('updateDirection', {direction: 'forward'})
				next();
			}
		},
		{
			name: 'needshop',
			path: '/needshop',
			component: NeedShop,
			beforeEnter: (to, from, next) => {
				store.commit('updateShowFooter', {showFooter: false})
				store.commit('updateDirection', {direction: 'forward'})
				SetTitle('搜索')
				next();
			},
			children: [{
				path: ':num', 
				component: NeedShop
			}]
		},
		{
			path: '/orders',
			component: Orders,
			beforeEnter: (to, from, next) => {
				store.commit('updateShowFooter', {showFooter: false})
				SetTitle('订单中心')
				next()
			},
			children: [{
					path: '', 
					component: Orders_list,
				},
				{
					path: '/orders/others', 
					component: Orders_others,
			}]
		},
		{
			path: '/orderDetail/:orderid',
			component: OrderDetail,
			beforeEnter: (to, from, next) => {
				store.commit('updateShowFooter', {showFooter: false})
				SetTitle('订单详情')
				next()
			}
		},
		{
			path: '/orderTrail/:orderid',
			component: OrderTrail,
			beforeEnter: (to, from, next) => {
				store.commit('updateShowFooter', {showFooter: false})
				SetTitle('订单跟踪')
				next()
			}
		},
		{
			path: '/snOrderList',
			component: snOrderList,
			beforeEnter: (to, from, next) => {
				store.commit('updateShowFooter', {showFooter: false})
				SetTitle('易购订单')
				next()
			}
		},
		{
			path: '/snOrderDetail/:orderid',
			component: snOrderDetail,
			beforeEnter: (to, from, next) => {
				store.commit('updateShowFooter', {showFooter: false})
				SetTitle('易购订单')
				next()
			}
		},
		{
			path: '/snOrderTrail/:orderid',
			component: snOrderTrail,
			beforeEnter: (to, from, next) => {
				store.commit('updateShowFooter', {showFooter: false})
				SetTitle('订单跟踪')
				next()
			}
		}
	]
})
/* 替换sessionStorage */
const myStorage = {
	data: {},
	setItem(key, value) {
		if (!key) return;

		this.data[key] = value;
	},
	getItem(key) {
		return this.data[key];
	},
	clear() {
		this.data = {};
	}
}
myStorage.clear()
let historyCount = myStorage.getItem('count') * 1 || 0
myStorage.setItem('/', 0)

router.beforeEach(function (to, from, next) {
  	store.commit('updateLoading', true)
	const toIndex = myStorage.getItem(to.path)
	const fromIndex = myStorage.getItem(from.path)

	if (toIndex) {
	    if (!fromIndex || parseInt(toIndex, 10) > parseInt(fromIndex, 10) || (toIndex === '0' && fromIndex === '0')) {
	      store.commit('updateDirection', {direction: 'forward'})
	    } else {
	      store.commit('updateDirection', {direction: 'reverse'})
	    }
	 } else {
	    ++historyCount
	    myStorage.setItem('count', historyCount)
	    to.path !== '/' && myStorage.setItem(to.path, historyCount)
		store.commit('updateDirection', {direction: 'forward'})
	}

	if (/\/http/.test(to.path)) {
		let url = to.path.split('http')[1]
		window.location.href = `http${url}`
	} else {
		next()
	}
})

router.afterEach(function (to) {
	store.commit('updateLoading', false)
})

export default router


